package com.uestc.servlet.ui;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uestc.admin.AdminBase;
import com.uestc.admin.AdminException;
import com.uestc.admin.AdminFactory;
import com.uestc.servlet.ServletBase;
import com.uestc.util.JsonUtil;
import com.uestc.util.Log;

@WebServlet("/admin/SearchAdmin")
public class SearchAdmin extends HttpServlet implements ServletBase {
	private static final long serialVersionUID = 1L;

	public SearchAdmin() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String opt = getParameter(request, "option");
		String conditions = getParameter(request, "conditions");
		String type = getParameter(request, "type");
		AdminBase admin = null;
		JsonUtil jsonUtil = new JsonUtil(conditions, JsonUtil.JSONOBJECT);
		jsonUtil.trim();
		try {
			switch (type) {
			case "user":
				admin = AdminFactory.getInstance(AdminFactory.USER_ADMIN);
				break;
			case "file":
				admin = AdminFactory.getInstance(AdminFactory.FILE_ADMIN);
				break;
			case "userlog":
				admin = AdminFactory.getInstance(AdminFactory.USERLOG_ADMIN);
				break;
			case "system-log-admin":
				admin = AdminFactory.getInstance(AdminFactory.SYSTEM_LOG_ADMIN);
			break;
			default:
				Log.logger.error("没有输入正确的查询信息");
				return;
			}
		} catch (AdminException e1) {
			Log.logException(e1);
			return;
		}
		if ("columnInfo".equals(opt)) {
			String columnInfo = admin.getColumns();
			Log.logger.info(columnInfo);
			writeMessage(response, columnInfo);
			return;

		} else if ("content".equals(opt)) {
			String start = getParameter(request, "start");
			String limit = getParameter(request, "limit");
			try {
				String msg = admin.getInfoWithCondition(Integer.valueOf(start), Integer.valueOf(limit),
						jsonUtil.toHashMap(), request);
				writeMessage(response, msg);
			} catch (Throwable e) {
				Log.logException(e);
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

}
